package com.bocloud.cmp.driver.providers.impl;

import org.springframework.stereotype.Service;

import com.bocloud.cmp.driver.config.Configuration;
import com.bocloud.cmp.driver.providers.FlavorDriver;
import com.bocloud.cmp.driver.providers.intf.FlavorProvider;
import com.bocloud.cmp.driver.providers.os.OSFlavorDriver;
import com.bocloud.cmp.driver.providers.os.OSDriver.Version;
import com.bocloud.cmp.driver.utils.VendorConverter;
import com.bocloud.cmp.enums.VendorType;
import com.bocloud.cmp.model.Butler;
import com.bocloud.cmp.model.FlavorModel;
import com.bocloud.common.model.BsmResult;

@Service("flavorProvider")
public class FlavorProviderImpl implements FlavorProvider {

	@Override
	public BsmResult sync(String vendor, String region) {
		FlavorDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		VendorType type = VendorConverter.convert(butler.getCategory());
		switch (type) {
		case OPENSTACK:
			driver = new OSFlavorDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
					butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
			break;
		default:
			break;
		}
		if (null == driver) {
			return new BsmResult(false, "暂不支持！");
		} else if (!driver.accessable()) {
			return new BsmResult(false, "用户鉴权失败！");
		} else {
			return driver.sync();
		}
	}

	@Override
	public BsmResult create(String vendor, FlavorModel flavor) {
		BsmResult result = null;
		FlavorDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		driver = new OSFlavorDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
				butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
		result = driver.create(flavor);
		return result;
	}

	@Override
	public BsmResult remove(String flavorId, String vendor) {
		BsmResult result = null;
		FlavorDriver driver = null;
		Butler butler = Configuration.instance().getConfig(vendor);
		driver = new OSFlavorDriver(butler.getUrl(), butler.getUsername(), butler.getPassword(), butler.getTenant(),
				butler.getDomainName(), butler.getProjectName(), Version.valueOf(butler.getVersion()));
		result = driver.remove(flavorId);
		return result;
	}

}
